<script setup lang="ts">
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate, onBeforeRouteLeave } from 'vue-router'
import { watch, watchEffect } from 'vue'
import useStore from '@/store'
import GoodsItem from './components/goods-item.vue'

const route = useRoute()
const { category, home } = useStore()
category.getCategory(route.params.id as string)
home.homeBanner()
/*
计算属性：依赖一个值或者多个值产生一个新的值
vue2 beforeRouteUpdate
vue3 onBeforeRouteUpdate
watch:侦听器，侦听数据的变化 
const stop=watch(需要侦听的数据,(newVal,oldVal)=>{},{
  deep:深度侦听
  immediate:是否初次定义时就执行回调
  flush:'pre:渲染前','post:渲染后','sync:修改一次调用一次'
})
需要侦听的数据:()=>需要侦听的数据
const obj=ref({
  name:'zs',
  age:18
})
watch(()=>obj.value.name)

*/

/*
1：beforeRouteUpdate
2:watch+注意销毁(beforeRouteLeave)
3:key(router-view)
*/

// const getData = () => {
//   console.log(route.params.id)
// }
// getData()
// watchEffect：
/*
watchEffect(()=>{
  只要在这里使用了响应式数据，只要数据变化了，它都会再次执行l回调函数，初次定义时就会执行回调函数
})
*/
// const stop = watch(
//   () => route.params.id,
//   newVal => {
//     console.log(newVal)
//     // 通过id调用接口获取详情数据
//   },
//   {
//     immediate: true
//   }
// )
// onBeforeRouteLeave(() => {
//   stop()
// })
// watchEffect(() => {
//   console.log('id', route.params.id)
// })
/*
1：在pinia定义state与action获取并存储数据
2:一级类目页面触发actions
3:定义数据类型
4：渲染
*/
</script>
<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBreadcrumb>
        <XtxBreadcrumbItem to="/">首页</XtxBreadcrumbItem>
        <XtxBreadcrumbItem>{{ category.categoryInfo.name }}</XtxBreadcrumbItem>
      </XtxBreadcrumb>
      <!-- 轮播图 -->
      <XtxCarousel style="height: 500px" :list="home.bannerList"></XtxCarousel>
      <!-- 1：专利 2：个性化更强 -->
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类:</h3>
        <ul>
          <li v-for="i in category.categoryInfo.children" :key="i.id">
            <a href="javascript:;">
              <img v-lazyImg="i.picture" />
              <p>{{ i.name }}</p>
            </a>
          </li>
        </ul>
      </div>

      <!-- 分类关联商品 -->
      <div
        class="ref-goods"
        v-for="i in category.categoryInfo.children"
        :key="i.id"
      >
        <div class="head">
          <h3>- {{ i.name }} -</h3>
          <p class="tag">{{ i.saleProperties }}</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem v-for="sub in i.goods" :key="sub.id" :goods="sub" />
        </div>
      </div>
    </div>
  </div>
  <div></div>
</template>
<style lang="less" scoped>
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}

.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
  }
}
</style>
